package org.jadapter.tests;

import static org.junit.Assert.assertEquals;

import org.jadapter.JAdapter;
import org.jadapter.examples.foobar.Bar;
import org.jadapter.examples.foobar.Foo;
import org.jadapter.examples.foobar.FooBar;
import org.jadapter.examples.foobar.FooBean;
import org.jadapter.registry.TransitiveAdapterRegistry;
import org.jadapter.tests.examples.AnnotatedFoo;
import org.jadapter.weavers.AnnotationWeaver;
import org.jadapter.weavers.Weaver;
import org.junit.Test;

public class TestAnnotationWeaver {

    @Test
	public void shouldProxyAnnotatedClasses() {

        TransitiveAdapterRegistry registry = new TransitiveAdapterRegistry(
                new JAdapter(FooBar.class)
        );

        Weaver weaver = new AnnotationWeaver(registry, registry);                 
     
        //Foo foo = weaver.weave(new FooBean(2));
        Foo proxy = weaver.weave(new AnnotatedFoo());
        
        // FooBean is annotated allowing us to transform it 
        Bar bar = (Bar)proxy;
          
        assertEquals(8, bar.bar());
        
    }
    
    
    @Test(expected=ClassCastException.class)
	public void shouldNotProxyNormalClasses() {

        TransitiveAdapterRegistry registry = new TransitiveAdapterRegistry(
                new JAdapter(FooBar.class)
        );

        Weaver weaver = new AnnotationWeaver(registry, registry);                 
     
        Foo proxy = weaver.weave(new FooBean(2));
        
        // FooBean is not annotated, resulting in a normal class cast exception
        Bar bar = (Bar)proxy; 
        
    }
	
}
